<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/7/23
 * Time: 14:52
 */
namespace app\jujin\controller;

use app\admin\service\SystemCateService;
use app\api\service\MemberService;
use controller\JunjinAdmin;
use service\DataService;
use service\LogService;
use service\ToolsService;
use service\WechatService;
use think\Db;
use think\view\driver\Think;

class Demandoffer extends JunjinAdmin
{
    public $table='demand_offer';

    public function index()
    {
        $this->title = '投标管理';
        $get = $this->request->get();
        $db = Db::name($this->table);
        if (isset($get['order_sn']) && $get['order_sn'] !== '') {
            $db->whereLike('order_sn', $get['order_sn']);
        }
        return parent::_list($db->order('id desc'));
    }
    /**
     * 启用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function choose()
    {
        if (DataService::update($this->table)) {

            $offer_id=$this->request->param('id');
            $offer=Db::name('demand_offer')
                ->where('id',$offer_id)
                ->field('mid,demand_id')
                ->find();
            $demand=Db::name('member_demand')
                ->where('id',$offer['demand_id'])
                ->where('status',1)
                ->field('title,mobile')
                ->find();
            //推送模板消息
            $offer_member=Db::name('store_member')
                ->where('id',$offer['mid'])
                ->field('id,openid')
                ->find();
            $redis=ToolsService::get_redis();
            $param['touser']=$offer_member['openid'];
            $param['template_id']='9YjljzCrcgpYSPi5cfq1-dz_a8H6AxlWuwZWydCKoQw';
            $param['page']='pages/nf_my_qd_demand_list/nf_my_qd_demand_list';
            $param['form_id']=$redis->rPop('jujin_wemini_form_id_list_'.$offer_member['id']);
            $param['data']=[
                'keyword1'=>[
                    'value'=>$demand['title']
                ],
                'keyword2'=>[
                    'value'=>$demand['mobile']
                ]
            ];
            if(!$param['form_id']){
                LogService::write('模板消息',$offer_member['id'].':FormId不足，微信推送失败');
            }else{
                try{
                    $tmp=WechatService::WeMiniTemplate()->send($param);
                }catch (\Exception $e){
                    LogService::write('模板消息',$e->getMessage());
                }
            }

            $this->success("入围成功！", '');
        }
        $this->error("入围失败，请稍候再试！");
    }

}